package chat

import (
	"sanguo/cluster"
	"sanguo/node/node_chat"
	ss_rpc "sanguo/protocol/ss/rpc"
	"sanguo/rpc/enterChat"

	"github.com/golang/protobuf/proto"
)

type EnterChat struct {
}

func (this *EnterChat) OnCall(replyer *enterChat.EnterChatReplyer, arg *ss_rpc.EnterChatReq) {

	node_chat.Debugln("OnCall EnterChat", arg)

	chatMgr.PostTask(func() {

		r := &Role{}
		r.RoleID = arg.GetRoleID()
		r.UserID = arg.GetUserID()
		r.Level = arg.GetLevel()
		r.Name = arg.GetName()
		r.Game = cluster.PeerID(arg.GetGame())
		r.Portrait = arg.GetPortrait()

		ok := chatMgr.EnterChat(r)

		resp := &ss_rpc.EnterChatResp{}
		if ok {

			resp.Ok = proto.Int32(1)
			node_chat.Infoln("EnterChat OK", arg.GetUserID())
		} else {
			resp.Ok = proto.Int32(0)
			node_chat.Infoln("EnterChat Fail", arg.GetUserID())
		}
		replyer.Reply(resp)
	})
}

func init() {
	enterChat.Register(&EnterChat{})
}
